import { MessageBox,Message } from 'element-ui';
// 配置axios
import axios from "axios";
import strong from './strong'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'

let http = axios.create({
    baseURL:"/api",
    timeout:'5000',
    withCredentials:true
})

// 配置全局请求拦截
http.interceptors.request.use(config => {
    nprogress.start()
    let token = strong.get('qf-token')
    // console.log(token);
    config.headers['authorization'] = token
    return config
})




// 配置全局的响应拦截
http.interceptors.response.use(config => {
    nprogress.done()
    // console.log('config',config);

    let {state,msg} = config.data

    if(!state && config.config.url !== "/order/query_order_status"){
        MessageBox({
            message:msg,
            title:'错误',
            type:'error'
        })
    }

    let {code} = config.data
    if(code==='10022' || code==='1004'){
        Message.error('登录过期，请重新登录')
        strong.rm('qf-token')
        location.reload()
    }

    return config
})

export default http